<template>
	 <ex-dialog v-model="visible" draggable :title = titleData
	:width=700 :contentHeight=500>

	  <template #default>
		<el-tabs v-model="activeName"  style="height: 100%;">
		  <el-tab-pane label="基本信息" name="base" style="height: 100%">
			<el-row :gutter="80">
			  <el-col :lg="24">
				<el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="130px"  label-position="right">
					<el-form-item label="任务名称" prop="name">
						<el-input v-if="isCanEdit" :disabled = !isCanEdit v-model="form.name" clearable placeholder="任务名称"></el-input>

						<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :value="form.name "></ex-label>
						</view>
					</el-form-item>
				  <el-form-item label="任务类型" prop="taskType">
					  <ex-select  v-if="isCanEdit" :disabled = !isCanEdit v-model="form.taskType" dict-code="TASK_TYPE" placeholder="请选择任务类型" ></ex-select>
					 <view  v-if="!isCanEdit" class="contentlabel">
						<ex-label :code="'TASK_TYPE'" :value="form.taskType"></ex-label>
					 </view>
					</el-form-item>
				  <el-form-item label=是否含账外资产 prop="isIncludeInvisible">
					  <ex-select v-if="isCanEdit" :disabled = !isCanEdit v-model="form.isIncludeInvisible" dict-code="IS_INCLUDE_INVISIBLE"></ex-select>
					  	<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :code="'IS_INCLUDE_INVISIBLE'" :value="form.isIncludeInvisible??'暂无'"></ex-label>
						</view>
					</el-form-item>
					<el-form-item label="排除盘点资产状态" prop="excludeAssetStatus">
						<ex-select   v-if="isCanEdit" :disabled = !isCanEdit v-model="excludeAssetStatus"
						multiple clearable :ops="filterAssetStatus" placeholder="请选择排除盘点资产状态" ></ex-select>
						<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :value="viewAssetStatus"></ex-label>
						</view>
					</el-form-item>
					<el-form-item label="排除业务中资产" prop="checkAssetDocumentType">
						<ex-select  v-if="isCanEdit" :disabled = !isCanEdit v-model="checkAssetDocumentType"
						 multiple clearable :ops="checkAssetIncludeDocumentType" placeholder="请选择排除业务中资产" ></ex-select>
						<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :value="viewAssetDocumentType"></ex-label>
						</view>
					</el-form-item>
					<el-form-item label="开始日期"  prop="startDate">
						<el-date-picker v-if="isCanEdit" :disabled = !isCanEdit v-model="form.startDate"
										value-format="YYYYMMDD"
										type="date"
										placeholder="开始日期"
										date-format="YYYY-MM-DD"
										:teleported="false"
										:picker-options="startPickerOptions"
										placement="right-end"/>
						<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :value="dateFormat(form.startDate)"></ex-label>
						</view>
					</el-form-item>
					<el-form-item label="结束日期" prop="endDate">
						<el-date-picker  v-if="isCanEdit" :disabled = !isCanEdit v-model="form.endDate"
										value-format="YYYYMMDD"
										type="date"
										date-format="YYYY-MM-DD"
										placeholder="结束日期"
										:teleported="false"
										:picker-options="endPickerOptions"
										placement="right-end"/>
						<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :value="dateFormat(form.endDate)"></ex-label>
						</view>
					</el-form-item>
					<el-form-item label="说明" prop="description">
						<el-input v-if="isCanEdit"  :disabled = !isCanEdit v-model="form.description" :rows="3" type="textarea" clearable placeholder="说明"></el-input>

						<view  v-if="!isCanEdit" class="contentlabel">
							<ex-label :value="dateFormat(form.description??'暂无')"></ex-label>
						</view>
					</el-form-item>
				</el-form>
			  </el-col>
			</el-row>
		  </el-tab-pane>
			<el-tab-pane label="机构范围" name="ManageDept" style="height: 100%">
				<ex-rel-table ref="manageDeptTable" name="ManageDept" :rel-entity="form" :treeApi="this.$API.system.dept.treeData" default-expand-all></ex-rel-table>
			</el-tab-pane>
		  <el-tab-pane label="资产范围" name="Syscategory" style="height: 100%">
				<ex-rel-table ref="categoryRelTable" @change="categoryHandler" :rel-entity="form" :treeApi="this.$API.asset.assetcategory.treeData" default-expand-all></ex-rel-table>
		  </el-tab-pane>
			<el-tab-pane label="拍照范围" name="Syscategorys" style="height: 100%">
				<ex-tree-table ref="categoryPthoneTable" :rel-entity-id="form.id" :rel-entity-name="entityName" :rel-entity="assetCategoryPthone" :filter-data-list="this.assetCategoryPthoneIds"
				:treeApi="this.$API.asset.assetcategory.treeData" default-expand-all></ex-tree-table>
			</el-tab-pane>
		</el-tabs>
	  </template>
	  <template #footer>
		<div style="flex: auto;text-align: center">
			<template v-if="isCanEdit">
				<el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
				<el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
			</template>
		  <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
		</div>
	  </template>

	</ex-dialog>
  </template>

  <script>
  import scIconSelect from '@/components/scIconSelect'
  import ExRelTable from "@/components/exRelTable/index.vue";
  import ExLabel from "@/components/exLabel/index.vue";
  import ExSelect from "@/components/exSelect/index.vue";
  import exDialog from '@/components/exDialog';
  import ExTreeTable from "@/components/exTreeTable/index.vue";
  import tool from "@/utils/tool";
  import ExCheckBox from "@/components/exCheckBox/index.vue";
  import ExRadioGroup from "@/components/exRadioGroup/index.vue";
  import globalparam from "@/constant/model/globalparam";

  export default {
	emits: ['success'],
	components: {
		ExRadioGroup,
		ExCheckBox,
		ExTreeTable,
		ExSelect,
		ExLabel,
	  ExRelTable,
	  scIconSelect,
	  exDialog,
	},
	props: {
	  menu: {
		type: Object, default: () => {
		}
	  },
	},
	data() {
	  return {
		activeName: 'base',
		visible: false,
		  entityName: "FamsAssetCheckTask",
		form: {
			id: "",
			name: "",
			taskType: "",
			createOrg: "",
			status: "",
			startDate:"",
			endDate:"",
			description:"",
		},
		  button_rules: {
			  name: [{required: true, message: "请输入任务名称", trigger: "blur"},],
			  taskType: [{required: true, message: "请选择任务类型", trigger: "blur"},],
			  startDate: [{required: true, message: "请选择开始日期", trigger: "blur"},],
			  endDate: [{required: true, message: "请选择结束日期", trigger: "blur"},],
		  },
		  loading: false,
		  oldForm: null,
		  ManageDept: [],
		  manageAssetCategoryIds: [],
		  assetCategoryPthoneIds: [],//盘点资产范围
		  assetCategoryPthone:[],//资产拍照范围
		  titleData: '',
		  excludeAssetIds:['scrap','delete'],
		  isCanEdit: false,
		  excludeAssetStatus: [],
		  checkAssetIncludeDocumentType: [],
		  checkAssetDocumentType: [],
		  viewAssetDocumentType: '',//查看时排除业务中资产
		  filterAssetStatus: [],//资产状态
		  viewAssetStatus: '',//查看时排除资产状态
		  startPickerOptions: {
			  disabledDate: (date) =>{
				  if (this.form.endDate) {
					  return date.getTime() > new Date(this.form.endDate).getTime();
				  }
				  return false;
			  }
		  },
		  endPickerOptions: {
			  disabledDate: (date) =>{
				  if (this.form.startDate) {
					  return date.getTime() < new Date(this.form.startDate).getTime();
				  }
				  return false;
			  }
		  },
	  }
	},
	  mounted() {

	},
	  beforeMount() {
	  },
	  beforeCreate() {
	  },
	  onLoad() {},
	methods: {
		categoryHandler() {
			if (this.form.relMaps) {
				this.assetCategoryPthoneIds = this.form.relMaps.FamsAssetCategory;
			}
		},
		reset() {
			this.form = Object.assign({}, this.oldForm);
		},
		//显示
		async open(entity) {
			this.excludeAssetStatus = []
			this.checkAssetIncludeDocumentType = []
			this.checkAssetDocumentType = []
			this.filterAssetStatus = []//资产状态
			let documentTypes = await this.$cache.dic.options("DOCUMENT_TYPE", false);
			let assetStatusData = await this.$cache.dic.options("ASSET_STATUS", false);
			let checkAssetIncludeDocumentTypeData = await this.$cache.data.loadGlobalParam(globalparam.checkAssetIncludeDocumentType.paramCode);
			let checkAssetStatisticsRangeData = await this.$cache.data.loadGlobalParam(globalparam.checkAssetStatisticsRange.paramCode);
			this.filterAssetStatus = assetStatusData.filter(item => {
				return checkAssetStatisticsRangeData.includes(item.value);
			});
			this.checkAssetIncludeDocumentType = documentTypes.filter(item => {
				return checkAssetIncludeDocumentTypeData.includes(item.value);
			});
			this.form = {};
			this.assetCategoryPthone = [];
			this.assetCategoryPthoneIds = [];
			this.titleData = '';
			this.isCanEdit = false;
			this.assetStatus = [];
			this.form = Object.assign({}, entity.data);
			this.viewAssetStatus="";
			this.viewAssetDocumentType="";
			if (!entity.isCanEdit) {
				this.titleData = '查看任务'
				if (this.form.excludeAssetStatus) {
					let datas = this.form.excludeAssetStatus.split(",");
					datas.forEach(assetStatus => {
						if(checkAssetStatisticsRangeData.includes(assetStatus)){
							this.excludeAssetStatus.push(assetStatus);
						}
					})
					this.excludeAssetStatus.forEach(value=>{
						this.filterAssetStatus.forEach(item=>{
							if(item.value==value){
								if(this.viewAssetStatus==""){
									this.viewAssetStatus=this.viewAssetStatus+" "+item.label;
								}else{
									this.viewAssetStatus=this.viewAssetStatus+" "+item.label;
								}
							}
						});
					})
					if(this.viewAssetStatus==""){
						this.viewAssetStatus="暂无"
					}

				}

				if(this.form.excludeDocumentTypes){
					this.checkAssetDocumentType = this.form.excludeDocumentTypes.split(",");

					this.form.excludeDocumentTypes.split(",").forEach(value=>{
						this.checkAssetIncludeDocumentType.forEach(item=>{
							if(item.value==value){
								if(this.viewAssetDocumentType==""){
									this.viewAssetDocumentType=this.viewAssetDocumentType+" "+item.label;
								}else{
									this.viewAssetDocumentType=this.viewAssetDocumentType+" "+item.label;
								}
							}
						});
					})
				}
				if(this.viewAssetDocumentType==""){
					this.viewAssetDocumentType="暂无"
				}
				if(!this.isCanEdit){
					this.button_rules.name[0].required=false;
					this.button_rules.taskType[0].required=false;
					this.button_rules.startDate[0].required=false;
					this.button_rules.endDate[0].required=false;
				}


			} else {
				if (this.form.id) {
					this.titleData = '编辑任务'
					if (this.form.excludeAssetStatus) {
						let datas = this.form.excludeAssetStatus.split(",");
						this.excludeAssetStatus = datas.filter(assetStatus => {
							return this.excludeAssetIds.some(item => {
								return item !== assetStatus;
							})
						})
					}
					if(this.form.excludeDocumentTypes){
						this.checkAssetDocumentType = this.form.excludeDocumentTypes.split(",");
					}
				} else {
					this.titleData = '新增任务'
				}
				this.button_rules.name[0].required=true;
				this.button_rules.taskType[0].required=true;
				this.button_rules.startDate[0].required=true;
				this.button_rules.endDate[0].required=true;
			}
			this.isCanEdit = entity.isCanEdit;

			if (!this.form.assetCategoryPhoto) {
				this.assetCategoryPthone = [];
			} else {
				this.assetCategoryPthone = this.form.assetCategoryPhoto.split(",");
			}
			if (this.form.relMaps && this.form.relMaps.FamsAssetCategory && this.form.relMaps.FamsAssetCategory.length) {
				this.assetCategoryPthoneIds = this.form.relMaps.FamsAssetCategory;
			}
			this.oldForm = Object.assign({}, this.form);
			this.visible = true;
			this.activeName = "base";


		},
		//保存
		async save() {
			let valid  = await this.$refs.dialogForm.validate( valid => {return valid;});
			if (valid) {
				if(this.form.relMaps.FamsAssetCategory.length === 0){
					this.$message.warning("请勾选需要盘点的资产分类");
					return;
				}
				if(this.form.relMaps.ManageDept.length === 0){
					this.$message.warning("请勾选需要盘点的部门");
					return;
				}
				if(this.assetCategoryPthone){
					this.form.assetCategoryPhoto = this.assetCategoryPthone.join(",");
				}
				if(this.excludeAssetStatus){
					this.excludeAssetStatus.push("scrap");
					this.excludeAssetStatus.push("delete");
					this.form.excludeAssetStatus = this.excludeAssetStatus.join(",");
				}
				if(this.checkAssetDocumentType){
					this.form.excludeDocumentTypes = this.checkAssetDocumentType.join(",");
				}
				try {
					this.loading = true
					let rsp;
					let loginUser = this.$TOOL.data.get("USER_INFO");
					this.form.createOrg = loginUser.orgId;
					if (this.form.id) {
						rsp = await this.$API.asset.checkTaskApi.edit.post(this.form)
					} else {
						rsp = await this.$API.asset.checkTaskApi.addAysn.post(this.form)
					}
					if (rsp.code === 2000) {
						this.$emit('success');
						this.$message.success(this.$t("code."+rsp.code));
						this.visible = false;
						this.$store.dispatch("commit",this.form.entityName);
					} else {
						this.$message.warning(rsp.message)
					}
				} finally {
					this.loading = false
				}
			}
		},
		dateFormat(val){
			if(val.length>7){
				return val.substr(0,4)+"-"+val.substr(4,2)+"-"+val.substr(6,2);
			}else{
				return val;
			}
		},
	}
  }
  </script>

  <style scoped>
  h2 {
	font-size: 17px;
	color: #3c4a54;
	padding: 0 0 30px 0;
  }

  [data-theme="dark"] h2 {
	color: #fff;
  }
  .label-center{
	  display: flex;
	  justify-content: center;
	  align-items: center;
  }

  .contentlabel{
	border-bottom: 1px solid #e8f1ff;
	width: 100%;
  }
  </style>
